package interfejsy.logika.logika_wlasciwa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.JSONParser;
import model.Wynik;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class _Base_ extends AsyncTask<String,String,String>{
        private ProgressDialog pDialog/*=new ProgressDialog(ct)*/;
        private boolean TTT=false;
        Context ctx_;
            ProgressDialog dialog ;
        public boolean isTTT() {
                return TTT;
        }
        Activity aa;
        public void setAa(Activity aa) {
                this.aa = aa;
        }
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONParser jsonParser = new JSONParser();
        int wynik;
        public void clear(){
                jsonParser=null;
        }
        public int getWynik() {
                return wynik;
        }
        String url(String a){
                if(a!="" && a!=null)
                        return new BASE().metoda(a);
                else
                        return "";
        }
        Context ct;
        public _Base_(Activity aa,String[] aaa,Context ctx) {
                this.aa = aa;
                this.aaa = aaa;
                this.ct=ctx;
        }
        Map<String,String> cca=new HashMap<String, String>();
        /*public _Base_(MainActivity aa,Map<String,String> aaa) {
                this.qq = aa;
                this.cca = aaa;
        }*/
        public _Base_(Activity aa,Map<String,String> aaa,Context ctx) {
                this.aa = aa;
                this.cca = aaa;
                this.ct=ctx;
        }
        public _Base_(String b,Map<String,String> aaa,Activity ctx) {
                aa=ctx;this.cca = aaa;
        }
        public _Base_(Map<String,String> aaa,Context ctx) {
                
                this.ct=ctx;
                this.cca = aaa;
        }
        /*public _Base_(Fragment f,Map<String,String> aaa,Context ctx) {
                this.TTT=false;
                this.aa = f.getActivity();
                this.cca = aaa;
                this.ct=ctx;
        }*/
        public _Base_(Map<String,String> aaa) {
                this.TTT=false;
                this.cca = aaa;
                //tu
             /*   if ( !cca.containsKey("INFO") )
                {
                pDialog=new ProgressDialog(KlasaStatyczna.ctx___);
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.setMessage("KOMUNIKAT");
                }*/
        }
        /*public _Base_(Context c,Map<String,String> aaa) {
                this.TTT=false;
                this.ctx_=c;
                this.cca = aaa;
        }*/
        /*public _Base_(Fragment ffa,Map<String,String> aaa,String h) {
                this.TTT=false;
                this.aa = ffa.getActivity();
                this.cca = aaa;
                KlasaStatyczna.getDialog().show(ffa.getFragmentManager(), "TAG");
        }*/
        /**
         * wpisz tu w aaa klucz(lokalizacjaurl)
         *  logowanie--logowanie <br/>
                 rezerwowanie--rezerwowanie<br/> 
                 wyposarzenie--d_u_wyposarzenie <br/>
                 lokalizacja--lokalizacja <br/>
                 position--position <br/>
                 re--re <br/>
                 newroom--addobiect<br/> 
                 lista_rez--lista_rez <br/>
                 lista_potwierdzona--lista_rez<br/> 
                 zat--zat <br/>
         * @param aa
         * @param aaa
         * @param ctx
         * @param a
         */
        public _Base_(Activity aa,Map<String,String> aaa,Context ctx,int a) {
                this.aa = aa;
                this.cca = aaa;
                this.ct=ctx;
                this.aaa = new String[]{aaa.get("lokalizacjaurl")};
        }
        
        @Override
        protected void onPreExecute() {
                super.onPreExecute();
                /*pDialog=new ProgressDialog(KlasaStatyczna.ctx___);
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.setMessage("KOMUNIKAT");*/
                pDialog.show();
        }
        String[] aaa;
        public void setAaa(String[] aaa) {
                this.aaa = aaa;
        }
        @Override
        protected String doInBackground(String... arg0) {
                
                if(!cca.isEmpty())
                {
                        for (Object key : cca.keySet())
                                params.add(new BasicNameValuePair(key.toString(),cca.get(key)));
                }
                else
                {
                for (String c:aaa[1].split("!")){
                        String[] aa=c.split("-");
                        params.add(new BasicNameValuePair(aa[0],aa[1]));
                }
                }
                System.out.println("PRZZED JSON_"+TTT);String v=null;
                JSONObject json = jsonParser.makeHttpRequest(
                                "http://opiekuj2.javaprovider.net/php/__"+
                                cca.get("URL").toLowerCase()+".php",
                                "POST", params);
                if ( json!=null )
                        Wynik.json=json;
                TTT=true;
                if ( !cca.containsKey("INFO") ) {
                try {
                        Thread.sleep(2500);
                        System.out.println("CZEKAJ _Base_"+TTT);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
        }
                return null;
        }
        protected void onPostExecute(String file_url) {
                //KlasaStatyczna.getDialog().dismiss();
                if ( !cca.containsKey("INFO") )
                {
                        pDialog.dismiss();
                }
        }
}
